//let base_url = process.env.NODE_ENV === 'development'?'https://api.naomishu.cn/bbye.php/v1/':'https://api.naomishu.cn/bbye.php/'
// let axios = require('axios');
import Vue from 'vue';

import axios from 'axios';
import store from '../store';
import router from '../router';
const $this = Vue.prototype;
// let base_url = process.env.NODE_ENV === 'development' ? 'http://wangxiao.test/api' :'http://i.zhongchenjiaoyu.com/api'
let base_url = process.env.NODE_ENV === 'development' ? 'http://i.zhongchenjiaoyu.com/api' :'http://i.zhongchenjiaoyu.com/api'
let isFlag = false
let isToken = false
// 响应拦截器（处理响应数据）
axios.interceptors.response.use(
	response => {
		
		//登陆失效 , 终止
		if (response.data.status == '99' || response.data.status == '102') {
			if(response.data.status == '102'){
				$this.$message({
					message: response.data.msg,
					type: 'error',
				});
			}
		//   store.commit('removew_user_auth');
		  
		  
		  setTimeout(() => {
			localStorage.removeItem("activeMenu")
			router.push('/login')
		  },1500)
		//   console.log($this,$this.$router,'112221212121212');
			// $this.$router.push('/login');

		}else{
			if(response.status !== 200 || response.data.status !== 200){
				if(!isFlag){
					isFlag = true
					$this.$message({
						message: response.msg || response.data.msg,
						type: 'error',
					})
					setTimeout(() => {
						isFlag = false
					}, 1000);
				}
				
			}
		}

		return response;
	  },
	  error => {
		//使用axios.isCancel验证是否CancelToken
		if (axios.isCancel(error)) {
		  //命中缓存是不用loading的
		  return Promise.resolve(error.message);
		} else {
		  closeLoading()
		  $this.$message({
			message: '服务器错误,请联系管理员',
			type: 'error',
		  });
		  return Promise.resolve({
			data: {
			  code: 500,
			  msg: '服务器错误'
			}
		  });
		}
	  },
);
// 封装请求方法
function request({
	url,
	method = 'POST',
	data
}) {
	return new Promise((resolve, reject) => {
		let param = {}
		let company_id = store.state.company_id
		
		if(company_id){
			param = data?{...data,company_id:company_id}:{company_id:company_id} 
		}else{
			param = data?{...data}:{}
		}
		let student_id = localStorage.getItem('student_id')
		if(!param['student_id']&&student_id){
			param = {...param,student_id:student_id}
		}
		let headers = {
			'Authorization':localStorage.getItem('token'),
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', // 设置请求头
		}
		if(method == 'upload'){
			headers = {
                // 'Authorization':localStorage.getItem('token'),
                'Content-Type':'multipart/form-data', // 设置请求头
            }
		}
		axios({
			url: `${base_url}${url}`,
			method: 'POST',
			data: param,
			headers,
		}).then(res=>{
			if(res.data.status === 200){
				resolve(res.data);
			}else{
				resolve(res.data);
			}

		}).catch(err=>{
			reject(err)

		})
	});
}

// // 配置请求拦截器
// uni.addInterceptor('request', {
// 	// 在发送请求之前做一些处理
// 	config(requestConfig) {
// 		// 添加请求头、身份验证等
// 		// requestConfig.header.Authorization = 'Bearer ' + uni.getStorageSync('token');
// 		return requestConfig;
// 	},
// 	// 请求发生错误时的处理
// 	fail(error) {
// 		console.error('请求失败:', error);
// 	},
// });

export default request;